﻿namespace TwiMixer.RESTAPI.Sina
{
    using System;
    using System.IO;
    using System.Net;
    using System.Web;
    using Twitterizer;

    /// <summary>
    /// 渣浪api
    /// </summary>
    public class SinaRESTAPI : APIProvider
    {
        public SinaRESTAPI(OAuthTokens token, string callbackUrl)
        {
            RequestTokenUrl = "http://api.t.sina.com.cn/oauth/request_token";
            AuthorizeBaseUrl = "http://api.t.sina.com.cn/oauth/";
            AccessTokenUrl = "http://api.t.sina.com.cn/oauth/access_token";
            RESTBaseUrl = "http://api.t.sina.com.cn/";
            ConsumerKey = token.ConsumerKey;
            ConsumerSecret = token.ConsumerSecret;
            CallbackUrl = callbackUrl;

            ApiProviderType = APIProviderEnum.sina;
        }

        public override string GetOAuthAuthorizationUrl()
        {
            OAuthTokenResponse response = OAuthUtility.GetRequestToken(
                                                new Uri(RequestTokenUrl),
                                                ConsumerKey,
                                                ConsumerSecret,
                                                CallbackUrl);

            HttpContext context = HttpContext.Current;
            context.Response.Cookies[response.Token].Value = response.TokenSecret;

            Uri authorizationUri = OAuthUtility.BuildAuthorizationUri(AuthorizeBaseUrl, response.Token, false);
            return authorizationUri.ToString();
        }

        public override OAuthTokenResponse GetOAuthToken(string requestToken, string verifier, string requestTokenSecret)
        {
            OAuthTokenResponse response =
                OAuthUtility.GetAccessTokenDuringCallback(AccessTokenUrl, ConsumerKey, ConsumerSecret, requestTokenSecret);
            return response;
        }

        public override string GetStatusesHomeTimeline(int count, int since_id, int max_id, int page, bool trim_user, bool include_rts, bool include_entities, bool exclude_replies, bool contributor_details)
        {
            throw new NotImplementedException();
        }

        public override string GetStatusesHomeTimeline()
        {
            throw new NotImplementedException();
        }

        public override string GetRESTUsersShow(OAuthTokens token, string user_id)
        {
            token.ConsumerKey = ConsumerKey;
            token.ConsumerSecret = ConsumerSecret;
            WebRequestBuilder builder = new WebRequestBuilder(new Uri("http://api.t.sina.com.cn/users/show.json"), HTTPVerb.GET, token);

            builder.Parameters.Add("user_id", user_id);
            HttpWebResponse response = builder.ExecuteRequest();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            return reader.ReadToEnd();
        }

        public override string OAuthRequestToken(string oauth_callback)
        {
            OAuthTokens token = new OAuthTokens { ConsumerKey = this.ConsumerKey, ConsumerSecret = this.ConsumerSecret };
            WebRequestBuilder builder = new WebRequestBuilder(new Uri("http://api.t.sina.com.cn/oauth/request_token"), HTTPVerb.GET, token);
            builder.Parameters.Add("oauth_callback", oauth_callback);
            HttpWebResponse response = builder.ExecuteRequest();

            return
                new StreamReader(response.GetResponseStream()).ReadToEnd();
        }

        public override string OAuthAuthorize(string oauth_token)
        {
            Uri authorizationUri = OAuthUtility.BuildAuthorizationUri(AuthorizeBaseUrl, oauth_token, false);
            return authorizationUri.ToString();
        }

        public override string OAuthAccessToken(string oauth_token, string oauth_token_secret, string verifier)
        {
            OAuthTokens token = new OAuthTokens { ConsumerKey = this.ConsumerKey, ConsumerSecret = this.ConsumerSecret, AccessToken = oauth_token, AccessTokenSecret = oauth_token_secret };
            WebRequestBuilder builder = new WebRequestBuilder(new Uri("http://api.t.sina.com.cn/oauth/access_token"), HTTPVerb.GET, token);
            builder.Parameters.Add("oauth_verifier", verifier);
            HttpWebResponse response = builder.ExecuteRequest();

            return
                new StreamReader(response.GetResponseStream()).ReadToEnd();
        }

        public override string Statuses_HomeTimeline()
        {
            return string.Empty;
        }
    }
}